package com.example.myapplication;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

public class BannerViewPagerAdapter extends PagerAdapter {
    private Context mContext;
    private int[] mImages;
    public BannerViewPagerAdapter(Context context, int[] images) {
        mContext = context;
        mImages = images;
    }

    // 返回页面的个数
    @Override
    public int getCount() {
        return mImages.length;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    // 创建每一页的内容
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        // 加载布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.banner_item, container, false);

        // 通过id查找到组件对象,在view上查找
        ImageView imageView = view.findViewById(R.id.image);
        imageView.setImageResource(mImages[position]);

//        if(position == 0) {
//            imageView.setImageResource(R.drawable.banner1);
//        } else if(position == 1) {
//            imageView.setImageResource(R.drawable.banner2);
//        }
        // 设置图片


        container.addView(view);  // 把view加入到根组件当中
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
